package com.itfreer.bpm.model.service;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.config.dao.IBpmConfigDao;
import com.itfreer.bpm.config.entity.BpmConfigEntity;
import com.itfreer.bpm.config.entity.BpmTaskConfigEntity;
import com.itfreer.bpm.messager.BpmMessage;
import com.itfreer.bpm.model.api.EDeployType;
import com.itfreer.bpm.model.common.ModelCommonUtils;
import com.itfreer.bpm.model.dao.IModelDeployDao;
import com.itfreer.bpm.option.para.TaskInfo;
import com.itfreer.bpm.randomUtls.RandomUtils;
/**
 * 模型发布处理服务
 * @author gj
 *
 */
@Component("ModelDeployBaseService")
public class ModelDeployBaseService implements IModelDeployService{
	
	@Autowired
	IModelDeployDao modelDeployDao;
	@Autowired
	IBpmConfigDao bpmConfigDao;
	
	@Override
	public BpmMessage deployModel(String key) {
		EDeployType type= modelDeployDao.deployModel(key);
		BpmMessage message=new BpmMessage();
		
		Map<String,Object> where=new HashedMap<String,Object>();
		where.put("actdefid:=", key);
		List<BpmConfigEntity> lst= bpmConfigDao.getEntitys(null, where, null, 1000, 1);
		//1、如果是新发布的服务，添加流程配置信息 EDeployType.DeploySuccess.equals(type)
		if(lst==null || lst.size()<1) {
			BpmConfigEntity configE=new BpmConfigEntity();
			String actid=RandomUtils.createRandomGuid();
			configE.setActid(actid);
			configE.setActdefid(key);
			configE.setActdefname(modelDeployDao.getProcessName(key));
			
			Set<BpmTaskConfigEntity> taskconfigs=new LinkedHashSet<BpmTaskConfigEntity>();
			//1、U0启动
			BpmTaskConfigEntity u0_taskconfig=new BpmTaskConfigEntity();
			u0_taskconfig.setActid(actid);
			u0_taskconfig.setActdefid(key);
			u0_taskconfig.setTaskid(RandomUtils.createRandomGuid());
			u0_taskconfig.setTaskdefid("U0");
			u0_taskconfig.setTaskdefname("开始");
			u0_taskconfig.setOptbc(1);
			u0_taskconfig.setOptch(0);
			u0_taskconfig.setOptcx(0);
			u0_taskconfig.setOptgq(0);
			u0_taskconfig.setOptjg(0);
			u0_taskconfig.setOptjh(0);
			u0_taskconfig.setOptth(0);
			u0_taskconfig.setOpttj(1);
			u0_taskconfig.setOpttz(0);
			u0_taskconfig.setTaskorder(0);
			u0_taskconfig.setNotjbmb(ModelCommonUtils.NoticeJbmb);
			u0_taskconfig.setNotdbmb(ModelCommonUtils.NoticeDbmb);
			u0_taskconfig.setNotybmb(ModelCommonUtils.NoticeYbmb);
			u0_taskconfig.setRemmb(ModelCommonUtils.RemindersMb);
			
			taskconfigs.add(u0_taskconfig);
			
			List<TaskInfo> tasks=modelDeployDao.getProcessTask(key);
			int index=1;
			for(TaskInfo task:tasks) {
				BpmTaskConfigEntity taskconfig=new BpmTaskConfigEntity();
				taskconfig.setActid(actid);
				taskconfig.setActdefid(key);
				taskconfig.setTaskid(RandomUtils.createRandomGuid());
				taskconfig.setTaskdefid(task.getTaskdefinedid());
				taskconfig.setTaskdefname(task.getTaskName());
				taskconfig.setOptbc(1);
				taskconfig.setOptch(1);
				taskconfig.setOptcx(1);
				taskconfig.setOptgq(1);
				taskconfig.setOptjg(1);
				taskconfig.setOptjh(1);
				taskconfig.setOptth(1);
				taskconfig.setOpttj(1);
				taskconfig.setOpttz(1);
				taskconfig.setTaskorder(index);
				taskconfig.setNotjbmb(ModelCommonUtils.NoticeJbmb);
				taskconfig.setNotdbmb(ModelCommonUtils.NoticeDbmb);
				taskconfig.setNotybmb(ModelCommonUtils.NoticeYbmb);
				taskconfig.setRemmb(ModelCommonUtils.RemindersMb);
				
				taskconfigs.add(taskconfig);
				index++;
			}
			
			//20、U9结束
			BpmTaskConfigEntity u9_taskconfig=new BpmTaskConfigEntity();
			u9_taskconfig.setActid(actid);
			u9_taskconfig.setActdefid(key);
			u9_taskconfig.setTaskid(RandomUtils.createRandomGuid());
			u9_taskconfig.setTaskdefid("U9");
			u9_taskconfig.setTaskdefname("结束");
			u9_taskconfig.setOptbc(1);
			u9_taskconfig.setOptch(0);
			u9_taskconfig.setOptcx(0);
			u9_taskconfig.setOptgq(0);
			u9_taskconfig.setOptjg(0);
			u9_taskconfig.setOptjh(0);
			u9_taskconfig.setOptth(0);
			u9_taskconfig.setOpttj(1);
			u9_taskconfig.setOpttz(0);
			u9_taskconfig.setTaskorder(index);
			u9_taskconfig.setNotjbmb(ModelCommonUtils.NoticeJbmb);
			u9_taskconfig.setNotdbmb(ModelCommonUtils.NoticeDbmb);
			u9_taskconfig.setNotybmb(ModelCommonUtils.NoticeYbmb);
			u9_taskconfig.setRemmb(ModelCommonUtils.RemindersMb);
			taskconfigs.add(u9_taskconfig);
			
			configE.setTaskconfigs(taskconfigs);
			bpmConfigDao.add(configE);
		}else if(EDeployType.Failed.equals(type)){
			message.setStatus(false);
			message.setMessage("服务发布失败");
		}else if(EDeployType.RDeployFailed.equals(type)){
			message.setStatus(false);
			message.setMessage("服务重新发布失败");
		}else if(EDeployType.RDeploySuccess.equals(type)){
			message.setStatus(true);
			message.setMessage("服务重新发布成功");
		}else if(EDeployType.Deployed.equals(type)){
			message.setStatus(true);
			message.setMessage("服务已经发布，不需要重新发布");
		}
		return message;
	}
	
	
}
